/*
    $Header: /Windows/Shell/ShellExt.root/ShellExt/VC/ShellExt.cpp 2     08-07-24 0:20 Ken $
    $Log: /Windows/Shell/ShellExt.root/ShellExt/VC/ShellExt.cpp $
 * 
 * 2     08-07-24 0:20 Ken
 * add hook key (inject.dll setwindowshookex)
 *
 * 1     08-01-05 0:08 Ken

*/

// ShellExt.cpp : Implementation of DLL Exports.

#include "stdafx.h"
#include "resource.h"
#include "ShellExt.h"

#include "CShellExtModule.h"

BOOL HookHotKey();

/* move to head file for others use
class CShellExtModule : public CAtlDllModuleT< CShellExtModule >
{
public :
    DECLARE_LIBID(LIBID_ShellExtLib)
    DECLARE_REGISTRY_APPID_RESOURCEID(IDR_SHELLEXT, "{536584C5-F844-41B5-A99F-A7283B5222BC}")
};
*/

HINSTANCE g_hInstance;
CShellExtModule _AtlModule;

static HHOOK v_hhookKeyboadProcLL=NULL;

// DLL Entry Point
extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
    if(DLL_PROCESS_ATTACH==dwReason)
    {
        g_hInstance=hInstance;
		HookHotKey();
    }
    else if(DLL_PROCESS_ATTACH==dwReason)
    {
        if(v_hhookKeyboadProcLL!=NULL)
        {
            UnhookWindowsHookEx(v_hhookKeyboadProcLL);
        }
    }
    return _AtlModule.DllMain(dwReason, lpReserved);
}


// Used to determine whether the DLL can be unloaded by OLE
STDAPI DllCanUnloadNow(void)
{
    return _AtlModule.DllCanUnloadNow();
}


// Returns a class factory to create an object of the requested type
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
    return _AtlModule.DllGetClassObject(rclsid, riid, ppv);
}


// DllRegisterServer - Adds entries to the system registry
STDAPI DllRegisterServer(void)
{
    // registers object, typelib and all interfaces in typelib
    HRESULT hr = _AtlModule.DllRegisterServer();

    if(FAILED(hr))
    return hr;
/*
    // registering component category information in the Registry
    ICatRegister   *pcr;
    hr = CoCreateInstance(  CLSID_StdComponentCategoriesMgr,
                        NULL,
                        CLSCTX_INPROC_SERVER,
                        IID_ICatRegister,
                        (LPVOID*)&pcr);
    if(SUCCEEDED(hr))
    {
    // register deskband catalog
    hr = pcr->RegisterClassImplCategories(CLSID_ShTerm, 1, (CATID*)&CATID_DeskBand);
    pcr->Release();
    }
*/
    return hr;
}


// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer(void)
{
    HRESULT hr = _AtlModule.DllUnregisterServer();
/*
    // unregistering component category information in the Registry
    ICatRegister   *pcr;
    hr = CoCreateInstance(  CLSID_StdComponentCategoriesMgr,
                        NULL,
                        CLSCTX_INPROC_SERVER,
                        IID_ICatRegister,
                        (LPVOID*)&pcr);
    if(SUCCEEDED(hr))
    {
    // register deskband catalog
    hr = pcr->UnRegisterClassImplCategories(CLSID_ShTerm, 1, (CATID*)&CATID_DeskBand);
    pcr->Release();
    }
*/
    return hr;
}

BOOL HookHotKey()
{

    HINSTANCE hinstDLL;
    HOOKPROC hKeyboadProcLL;

    do
    {
        hinstDLL = LoadLibrary((LPCTSTR) "Inject.dll");
        if(NULL==hinstDLL)
        {
            ATLTRACE(_T("can't LoadLibrary Inject.dll\r\n"));
            break;
        }
        hKeyboadProcLL = (HOOKPROC)GetProcAddress(hinstDLL, "KeyboardProcLL");
        if(NULL==hKeyboadProcLL)
        {
            ATLTRACE(_T("can't GetProcAddress KeyboardProcLL\r\n"));
            break;
        }
        v_hhookKeyboadProcLL = SetWindowsHookEx(WH_KEYBOARD_LL, hKeyboadProcLL, hinstDLL, 0);
        if(NULL==v_hhookKeyboadProcLL)
        {
            ATLTRACE(_T("SetWindowsHookEx for WH_KEYBOARD_LL faild!\r\n"));
            break;
        }

        return TRUE;

    }while(0);

    if(NULL!=hinstDLL)
    {
        FreeLibrary(hinstDLL);
    }

    return FALSE;

}

